<template>
  <div class="apps">
    <div class="cars">
      admin logout
      <a-icon type="car" />
    </div>
    <div class="home">
      <span>Home / Goods</span>
    </div>
    <div class="sort">
      <p>
        Sort by: Default Price
        <a-icon type="arrow-up" />
      </p>
    </div>
    <div class="container">
      <div class="container-left">
        <p>P R I C E:</p>
        <p @click="allClick">All</p>
        <p @click="lowClick">0.00-100.00</p>
        <p @click="centerClick">100.00-500.00</p>
        <p @click="heightClick">500.00-1000.00</p>
        <p @click="onClick">1000.00-2000.00</p>
      </div>
      <div class="container-right">
        <div v-for="(item,index) in goodsList" :key="index" class="container-item">
          <img :src="item.productImage" alt />
          <p>{{item.productName}}</p>
          <p>{{item.salePrice}}</p>
          <span @click="addGoods(item)">加入购物车</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      goodsList: [],
      list: [],
      goods:[]
    };
  },
  created() {
    axios.get("http://localhost:8080/data.json").then((res) => {
      // console.log(res.data.result.list)
      this.goodsList = res.data.result.list;
      this.list = res.data.result.list;
    });

    let goods = localStorage.goods
    if(goods){
      this.goods = JSON.parse(goods)
    }
  },
  methods: {
    allClick() {
      axios.get("http://localhost:8080/data.json").then((res) => {
        // console.log(res.data.result.list)
        this.goodsList = res.data.result.list;
      });
    },
    lowClick() {
      this.goodsList = [];
      this.list.map((res) => {
        if (res.salePrice > 0 && res.salePrice < 100) {
          this.goodsList.push(res);
        }
      });
    },
    centerClick() {
      this.goodsList = [];
      this.list.map((res) => {
        if (res.salePrice > 100.0 && res.salePrice < 500.0) {
          this.goodsList.push(res);
        }
      });
    },
    heightClick() {
      this.goodsList = [];
      this.list.map((res) => {
        if (res.salePrice > 500.0 && res.salePrice < 1000.0) {
          this.goodsList.push(res);
        }
      });
    },
    onClick() {
      this.goodsList = [];
      this.list.map((res) => {
        if (res.salePrice > 1000.0 && res.salePrice < 10000.0) {
          this.goodsList.push(res);
        }
      });
    },
    addGoods(item) {
      // console.log(item);
      this.goods.push(item)
      console.log(this.goods)
      this.save()
    },
    save(){
      localStorage.goods = JSON.stringify(this.goods)
    }
  },
};
</script>

<style scoped>
@media screen {
  .apps {
    background: rgb(232, 232, 232);
  }
  .cars {
    width: 100%;
    font-size: 24px;
    text-align: right;
  }
  .home {
    background: rgb(240, 240, 240);
    height: 50px;
    line-height: 50px;
  }
  .home span {
    margin-left: 150px;
  }
  .sort {
    width: 100%;
    /* background: rosybrown; */
  }
  .sort p {
    width: 80%;
    margin: 0 auto;
    margin-top: 30px;
    background: rgb(240, 240, 240);
    text-align: right;
    height: 40px;
    line-height: 40px;
  }
  .container {
    width: 100%;
    display: flex;
    margin-top: 30px;
    justify-content: space-around;
  }
  .container-left {
    width: 25%;
    text-align: center;
  }
  .container-right {
    width: 70%;
    /* height: 100px; */
    /* border: 1px solid red; */
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }
  .container-item {
    width: 23%;
    padding: 5px;
    background: #fff;
    margin-top: 20px;
    /* height: 80px; */
    /* border: 1px solid black; */
  }
  .container-item img {
    width: 100%;
  }
  .container-item p {
    text-align: center;
  }
  .container-item span {
    display: inline-block;
    width: 100%;
    height: 30px;
    text-align: center;
    font-size: 22px;
    color: red;
    border: 1px solid red;
  }
}

@media screen and (max-width: 700px) {
  .cars {
    text-align: right;
  }
  .home span {
    margin-left: 20px;
  }
  .sort {
    width: 100%;
  }
  .sort {
    width: 80%;
    margin: 0 auto;
    text-align: right;
  }
  .container {
    width: 100%;
    border: 1px solid red;
    height: 100px;
  }
  .container-left {
    display: none;
  }
  .container-right {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
  }
  .container-right div {
    width: 100%;
  }
  .container-item {
    width: 100%;
    height: 200px;
    border: 1px solid red;
    display: flex;
    padding: 10px;
    overflow: hidden;
  }
  .container-item img {
    width: 30%;
    height: 200px;
    /* height: 100%; */
  }
  .container-item span {
    width: 150px;
    font-size: 14px;
    text-align: center;
    height: 30px;
    line-height: 30px;
    position: absolute;
    right: 10px;
    /* bottom: 5px; */
    margin-top: 130px;
  }
}
</style>